package com.wm.gridimageupload

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.config.SelectMimeType
import com.wm.gridimageupload.bean.ImageInfoBean
import com.wm.gridimageupload.widget.RoundProgress

class GridImageUpAdapter : RecyclerView.Adapter<GridImageUpAdapter.ViewHolder>() {

    private lateinit var context: Context

    private val TYPE_CAMERA = 1
    private val TYPE_PICTURE = 2

    private var selectMax = 8

    private var data: MutableList<ImageInfoBean> = mutableListOf()

    /**
     * 添加，删除图片的接口回调实例
     */
    private lateinit var mOnAddPicClickListener: onAddPicClickListener


    /**
     * 设置图片的最大张数
     */
    fun setContext(context: Context) {
        this.context = context
    }

    /**
     * 设置数据，刷新列表
     */
    fun setData(data: MutableList<ImageInfoBean>) {
        this.data = data
        notifyDataSetChanged()
    }

    /**
     * 添加图片的回调
     */
    fun setOnAddPicClickListener(mOnAddPicClickListener: onAddPicClickListener) {
        this.mOnAddPicClickListener = mOnAddPicClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_grid_filter_image,parent,false)
        return ViewHolder(view)
    }

    /**
     * item的数量
     */
    override fun getItemCount(): Int {
        if (data.size < selectMax) {
            return data.size + 1
        } else {
            return data.size
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // 不满8张，显示添加
        if (getItemViewType(position) == TYPE_CAMERA) {
            holder.layoutAdd.visibility = View.VISIBLE
            holder.imgClose.visibility = View.INVISIBLE
            holder.layoutAdd.setOnClickListener {
                mOnAddPicClickListener.onAddPicClick()
            }
        } else {
            val imageInfoBean = data[position]
            val localMedia = imageInfoBean.localMedia
            val mimeType = localMedia?.mimeType

            // 显示错误图
            if (imageInfoBean.getErrorType() == 0) {

            } else {
                holder.ivErr.visibility = View.GONE
                holder.imgClose.visibility = View.VISIBLE
                holder.imgClose.setOnClickListener {
                    data.removeAt(position)
                    notifyDataSetChanged()
                }

                if (PictureMimeType.getMimeType(mimeType) == SelectMimeType.ofImage()) {
                    Glide.with(context).load(imageInfoBean.getPath()).into(holder.mImg)
                }
            }
        }
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        // 图片
        val mImg: ImageView = itemView.findViewById(R.id.img)

        // 进度条
        val progress: RoundProgress = itemView.findViewById(R.id.progress)

        // 错误图片
        val ivErr: ImageView = itemView.findViewById(R.id.iv_err)

        // 添加图片布局
        val layoutAdd: LinearLayout = itemView.findViewById(R.id.layout_add)

        // 删除图片按钮
        val imgClose: ImageView = itemView.findViewById(R.id.img_close)

    }

    override fun getItemViewType(position: Int): Int {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA
        } else {
            return TYPE_PICTURE
        }
    }

    /**
     * 是否显示添加
     */
    private fun isShowAddItem(position: Int): Boolean {
        val size = data.size
        return position == size
    }

    /**
     * 添加，删除图片的接口
     */
    interface onAddPicClickListener {
        /**
         * 添加图片
         */
        fun onAddPicClick()

        /**
         * 删除图片
         *
         * @param position
         */
        fun onDeletePicClick(position: Int)
    }


}